<?php
/**
 * @Author: Administrator
 * @Date:   2015-11-25 03:58:00
 * @Last Modified by:   Administrator
 * @Last Modified time: 2015-11-25 09:09:34
 */
// 模板切换控制器

namespace Admin\Controller;
use Admin\Controller\CommonController;
use Common\Tool\Dir;

class ThemeController extends CommonController{

	public function index()
	{

		$dir = './Theme';
		$dirs = Dir::Tree($dir);
		// 前台配置文件路径
		$tmp = array();
		foreach ($dirs as $v) {
			//查看是否存在图片
			if($v['type']=='dir'){
				$jpg = $v['path'].'index.jpg';
				if(is_file($jpg)){
					$v['jpg']=__ROOT__.'/Theme/'.$v['filename'].'/index.jpg';
				}else{
					$v['jpg']=__ROOT__ . '/Public/' .MODULE_NAME. '/Images'.'/index.jpg';
				}
				$tmp[]=$v;
			}
		}
		$path = APP_PATH.'/Home/Conf/theme.php';
		$config = include_once $path;
		$nowtheme = $config['DEFAULT_THEME'];
		$this->assign('nowtheme',$nowtheme);
		// print_r($dirs);die;
		$this->assign('theme',$tmp);
		$this->display();
	}

	/**
	 * 设置模板
	 */
	public function setTheme()
	{
		$filename = I('get.dir');
		$path = APP_PATH.'/Home/Conf/theme.php';
		$str="<?php \nreturn array(\n\t'DEFAULT_THEME'=>$filename,\n);";
		// 写入配置
		if(!is_writable($path)){
			$this->error('配置文件不可写,请手动修改'.$path);
		}else{
			if(file_put_contents($path, $str)){
				$this->success('修改成功');
			}else{
				$this->error('修改失败');
			}
		}
	}

	
}